package com.resou.reader.paycenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.LoginData;
import com.resou.reader.base.v.BaseActivity;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.mine.consumerecord.ConsumeRecordActivity;
import com.resou.reader.mine.coupon.CouponActivity;
import com.resou.reader.mine.paycenter.PayCenterActivity;
import com.resou.reader.mine.rechargerecord.RechargeRecordActivity;
import com.resou.reader.paycenter.p.WalletPresenter;
import com.resou.reader.paycenter.v.IWalletView;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class WalletActivity extends BaseActivity<WalletPresenter> implements IWalletView {

    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvwallet)
    TextView tvwallet;
    @BindView(R.id.tvValidity)
    LinearLayout tvValidity;
    @BindView(R.id.btnRecharge)
    Button btnRecharge;
    @BindView(R.id.rlReCharge)
    RelativeLayout rlReCharge;
    @BindView(R.id.rlConsumeRecord)
    RelativeLayout rlConsumeRecord;
    @BindView(R.id.rlRewordRecord)
    RelativeLayout rlRewordRecord;
    @BindView(R.id.tvHotMoney)
    TextView tvHotMoney;
    @BindView(R.id.tvHotTicket)
    TextView tvHotTicket;
    @BindView(R.id.tvWalletNum)
    TextView tvWalletNum;
    WalletPresenter walletPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void initPresenter() {
        walletPresenter = new WalletPresenter(this);
    }

    @Override
    protected void initView() {
        LoginData loginData = UserInstance.getLoginData();
        if (loginData != null) {
            tvHotMoney.setText((int) loginData.getBlance() + "");
            tvHotTicket.setText((int) loginData.getGiftBlance() + "");
        } else {
            tvHotMoney.setText("0");
            tvHotTicket.setText("0");
        }

    }

    @Override
    protected void retryRefreshData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!mLoading.isAdded() && !mLoading.isRemoving() && !mLoading.isVisible()) {
            mLoading.show(getSupportFragmentManager());
        }
        walletPresenter.quearCouponNum();
    }

    @Override
    public Context getViewContext() {
        return this;
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, WalletActivity.class);
        context.startActivity(intent);
    }

    @OnClick({R.id.ivBack, R.id.tvValidity, R.id.btnRecharge, R.id.rlReCharge, R.id.rlConsumeRecord, R.id.rlRewordRecord})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvValidity:
                CouponActivity.start(this);
                break;
            case R.id.btnRecharge:
                startActivity(new Intent(this, PayCenterActivity.class));
                break;
            case R.id.rlReCharge:
                RechargeRecordActivity.startActivity(this);
                break;
            case R.id.rlConsumeRecord:
                ConsumeRecordActivity.startActivity(this);
                break;
            case R.id.rlRewordRecord:
                break;
        }
    }

    @Override
    public void showCouponNum(int num) {
        if (!NetworkUtils.isNetworkConnected(this)) {
            ToastUtil.makeShortToast("网络异常");
        }
        if (tvWalletNum != null) {
            tvWalletNum.setText("您有" + num + "张优惠券即将到期");
        }
        mLoading.dismiss();
    }

}
